package com.chaochis.wind.config.web_config;

import com.chaochis.wind.base_config.context_config.WindContext;
import com.chaochis.wind.config.secutiry_config.WindInterceptor;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Log4j2
public class WebSecurityConfig extends WebMvcConfigurationSupport {

  @Autowired
  private SecurityProperties securityProperties;

  @SneakyThrows
  @Override
  public void addInterceptors(@NonNull InterceptorRegistry registry) throws ExceptionInInitializerError{
    if (this.securityProperties.isEnable()) {
      for(var item : this.securityProperties.getHandler()){
        WindInterceptor webAdminFilter = (WindInterceptor) WindContext.getBean(item);
        var webRegistration = registry.addInterceptor(webAdminFilter);
        webAdminFilter.onAddToInterceptorRegistry(webRegistration);
        webRegistration.excludePathPatterns("/error");
      }
    } else {
      log.info("您未开启权限系统!");
    }

  }

}
